﻿/********************************************************************
	文件：Shape.cs
    作者：Pangdudu
    日期：2021/03/02
    描述: 生成mesh的形状
*********************************************************************/

using UnityEngine;

namespace ShapeMesh
{
    public abstract class Shape
    {
        #region protected members
        protected MeshPivot _meshPivot;//轴心的位置
        protected string _meshName;

        protected Vector3 _vertexOffset;//顶点的偏移量
        protected Vector3[] _vertices;//顶点数据
        protected Vector3[] _normals;//法线数据
        protected int[] _triangles;//三角面顶点的索引数据
        protected Vector2[] _uvs;//uv坐标数据

        protected Mesh _shapeMesh;//保存生成的mesh
        #endregion

        #region public properties
        /// <summary>
        /// 轴心的位置
        /// </summary>
        public MeshPivot MeshPivot => _meshPivot;

        /// <summary>
        /// 顶点数据
        /// </summary>
        public Vector3[] Vertices
        {
            get
            {
                if (_vertices == null) _vertices = GetVertices();
                return _vertices;
            }
        }

        /// <summary>
        /// 法线数据
        /// </summary>
        public Vector3[] Normals
        {
            get
            {
                if (_normals == null) _normals = GetNormals();
                return _normals;
            }
        }

        /// <summary>
        /// 三角面顶点的索引数据
        /// </summary>
        public int[] Triangles
        {
            get
            {
                if (_triangles == null) _triangles = GetTriangles();
                return _triangles;
            }
        }

        /// <summary>
        /// uv坐标数据
        /// </summary>
        public Vector2[] UVs
        {
            get
            {
                if (_uvs == null) _uvs = GetUVs();
                return _uvs;
            }
        }

        /// <summary>
        /// 生成的mesh数据
        /// </summary>
        public Mesh ShapeMesh
        {
            get
            {
                if (_shapeMesh == null) _shapeMesh = GenerateMesh();
                return _shapeMesh;
            }
        }
        #endregion

        #region ctor
        protected Shape(MeshPivot meshPivot, string meshName)
        {
            _meshPivot = meshPivot;
            _meshName = meshName;
        }
        #endregion

        #region protected functions
        /// <summary>
        /// 生成mesh
        /// </summary>
        /// <returns></returns>
        protected Mesh GenerateMesh()
        {
            var mesh = new Mesh {name = _meshName};

            _vertices = GetVertices();
            _normals = GetNormals();
            _triangles = GetTriangles();
            _uvs = GetUVs();

            mesh.vertices = _vertices;
            mesh.normals = _normals;
            mesh.triangles = _triangles;
            mesh.uv = _uvs;
            mesh.RecalculateTangents();
            mesh.RecalculateBounds();
            mesh.Optimize();
            return mesh;
        }
        #endregion

        #region abstract
        /// <summary>
        /// 根据mesh中心点的位置，获得顶点位置的偏移量
        /// </summary>
        /// <returns></returns>
        protected abstract Vector3 GetVertexOffset();

        /// <summary>
        /// 获得顶点的数据集合
        /// </summary>
        /// <returns></returns>
        protected abstract Vector3[] GetVertices();

        /// <summary>
        /// 获得法线方向的数据集合
        /// </summary>
        /// <returns></returns>
        protected abstract Vector3[] GetNormals();

        /// <summary>
        /// 获得三角面顶点的索引
        /// </summary>
        /// <returns></returns>
        protected abstract int[] GetTriangles();

        /// <summary>
        /// 获得UV坐标的数据集合
        /// </summary>
        /// <returns></returns>
        protected abstract Vector2[] GetUVs();

        #endregion

        #region protected static functions

        /// <summary>
        /// 获得圆心切割的边数(按照固定长度)
        /// </summary>
        /// <param name="radius">半径</param>
        /// <param name="arcLen">弧度的长度</param>
        /// <returns></returns>
        protected static int GetCircularSideCount(float radius, float arcLen = 0.1f)
        {
            const int minSideCount = 3;
            const int maxSideCount = 3000;

            var sideCount = Mathf.RoundToInt(Mathf.PI * 2 * radius / arcLen);
            sideCount = Mathf.Min(Mathf.Max(minSideCount, sideCount), maxSideCount);
            return sideCount;
        }

        #endregion

    }
}